package com.ruoyi.system.task;


import com.ruoyi.common.utils.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author xu wei
 * @date 2023/10/30
 * @version 1.0
 * @since jdk-1.8
 */
public class ByExportProcessorHelper {

    //替换字符串中的  回车符和空格
    private Pattern N_PATTREN = Pattern.compile("\r|\n|\t");
    private String keywordRegex = "《(.*?)》";
    private Pattern keywordPattern = Pattern.compile(keywordRegex);

    /**
     * 读取文件转为字符串并去除标签
     * @return
     */
//    ProcessResult filetostrUnlable(File xmlFile) {
//        if (null == xmlFile || !xmlFile.exists()) {
//            return null;
//        }
//
//        BufferedReader reader = null;
//        StringBuffer res = new StringBuffer();
//        List<SysKeyword> bookNameList = new ArrayList<>();
//        try {
//            reader = new BufferedReader(new InputStreamReader(Files.newInputStream(xmlFile.toPath())));
//            String line;
//            Pattern pattern = Pattern.compile("<.*?>");
//            while ((line = reader.readLine()) != null) {
//                line = line.trim();
//                Matcher matcher = pattern.matcher(line);
//                while (matcher.find()) {
//                    line = line.replace(matcher.group(), "");
//                }
//                if (StringUtils.isNotEmpty(line)) {
//                    res.append(line).append("\n");
//
//                    Matcher keypwordMatcher = keywordPattern.matcher(line);
//                    while (matcher.find()) {
//                        String bookTitle = matcher.group(1);
//                        SysKeyword keyword = new SysKeyword();
//                        keyword.setKeyWord(bookTitle);
//                        keyword.setWordType(SysKeywordType.HPR.code);
//                        keyword.setCreateBy(ByExportProcessorHelper.class.getName());
//                        keyword.setCreateTime(new Date());
//                        keyword.setUpdateBy(ByExportProcessorHelper.class.getName());
//                        keyword.setUpdateTime(new Date());
//                        keyword.setDelFlag(0);
//                        bookNameList.add(keyword);
//                    }
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        } finally {
//            if (reader != null) {
//                try {
//                    reader.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//
//        ProcessResult result = new ProcessResult();
//        result.content = res.toString();
//        result.bookNameList = bookNameList;
//        return result;
//    }

    /**
     * 比较目录大小
     * @param s1
     * @param s2
     * @return
     */
    public int compareCata(String s1, String s2) {
        try{
            String[] xs = s1.split("\\.");
            String[] ys = s2.split("\\.");
            int level = Math.max(xs.length, ys.length);
            String a1 = "";
            for (String s : xs) {
                a1 += StringUtils.leftPad(s.trim(), 2, "0");
            }
            a1 = StringUtils.rightPad(a1, 2 * level, "0");
            String a2 = "";
            for (String s : ys) {
                a2 += StringUtils.leftPad(s.trim(), 2, "0");
            }
            a2 = StringUtils.rightPad(a2, 2 * level, "0");
            return Long.compare(Long.parseLong(a1), Long.parseLong(a2));
        }catch (Exception e){
            return 1;
        }
    }

    /**
     * 读取excel为html，并且替换原文本
     *
     * @param text 读取的原行文本
     * @param tablePath 表格路径
     */
//    public String readExcelToHtml(String text, String tablePath) {
//        String tableHtml = ExcelToHtml.readExcelToHtml(new File(tablePath), true);
//        if(StringUtils.isNotEmpty(tableHtml)){
//            int start = text.indexOf("<image");
//            int end = Math.max(text.indexOf("/>")+"/>".length(),text.indexOf("</image>")+"</image>".length());
//            return text.replace(text.substring(start,end),tableHtml).replace("<title xml:lang=\"en\"/>","");
//        }
//        return null;
//    }

//    public class ProcessResult {
//        public String content;
//        public List<SysKeyword> bookNameList;
//    }

    /**
     * 替换字符串中的 回车符和空格
     * @param str
     * @return
     */
    String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Matcher m = N_PATTREN.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    /**
     * 从xml标签中获取内容文本
     * @param itemLine
     * @return
     */
    public String getTextFromSingleXML(String itemLine) {
        try {
            return itemLine.replaceAll("<.*?>","");
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 拼接数字.
     * @param str
     * @return
     */
    public String getOnlyDigits(String str) {
        StringBuilder sb = new StringBuilder();
        str = str.replaceAll("、",".").replaceAll(" ","");
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (Character.isDigit(c) || c == '.') {
                sb.append(c);
            }else{
                break;
            }
        }
        return sb.length() ==0 ? "0" : sb.toString();
    }

    /**
     * 从目录行数据字符串获取数字   1.1xxxx -> 1.1
     * @param line
     * @return
     */
    String getDigitalFromLine(String line) {
        line = line.trim();
        line = getTextFromSingleXML(line);
        StringBuilder res = new StringBuilder();
        for (char c : line.toCharArray()) {
            if (Character.isDigit(c) || c == '.') {
                res.append(c);
            } else {
                break;
            }
        }
        return res.toString();
    }

    void lable(Map<String, List<String>> res) {
        List<String> chapterList = res.get("chapterList");
        List<String> cataList = res.get("cataList");
        Pattern pattern = Pattern.compile("<.*?>");
        for (String chapter : chapterList) {
            Matcher matcher = pattern.matcher(chapter);
            while (matcher.find()) {
                chapter = chapter.replace(matcher.group(), "");
            }
            /*//去除开头的\n
            chapter = chapter.replaceFirst("^\\n{1}","");*/
            chapter += "\n";
            System.out.print(chapter);
        }
        System.out.println("===============================================================");
        for (String cata : cataList) {
            Matcher matcher = pattern.matcher(cata);
            while (matcher.find()) {
                cata = cata.replace(matcher.group(), "");
            }
            cata += "\n";
            System.out.print(cata);
        }
    }
}
